var timer_my_request;

function showRequestDetail(requestId) {
	try {
		requestUrl = "customer_json_servlet";
		var params = {
			request_method:"get_request_detail",
			request_id:requestId
		};
		
		$.post(requestUrl, params, function(data){
			try {
				var requestDetail = data.resultData;
				var type = (requestDetail.type == 1 ) ? "<求帮助>" : "<求养护>";
				
				$("#div_maintain_request_detail").html("");
				$("#div_maintain_request_detail").css("display", "");
				var innerHtml = "";
				clearInterval(timer_my_request);
		
				innerHtml += '	<div style="width: 390px; height: 50px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;">';
				innerHtml += '		<span style="padding-top: 14px;">需求信息' + type + '</span>';
				innerHtml += '	</div>';
				if (requestDetail.remainTime > 0) {
					innerHtml += '	<div style="padding-top: 15px; font-size: 14pt; color: red;">';
					innerHtml += '		<span style="width: 115px; display: inline-block;">协助倒计时:</span>';
					innerHtml += '		<span id="remain_time"></span>';
					innerHtml += '	</div>';
				}
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">车辆信息:</span>';
				innerHtml += '		<span>' + requestDetail.carModel + '</span>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">当前位置:</span>';
				innerHtml += '		<span>' + requestDetail.address + '</span>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">联系方式:</span>';
				innerHtml += '		<span>' + requestDetail.customerMobile + '</span>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">求助内容:</span><br/>';
				innerHtml += '		<textarea style="width: 379px; height: 113px; border: none; background: url(\'../image/bg_info_textarea.png\') no-repeat;" disabled="disabled">' + requestDetail.content + '</textarea><br/>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="width: 390px; height: 10px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;"></div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 14pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">留言:</span><br/>';
				innerHtml += '	</div>';
				innerHtml += '	<div id="request_messages" align="left"></div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<textarea id="request_message" style="width: 379px; height: 113px; border: none; background: url(\'../image/bg_info_textarea.png\') no-repeat;"></textarea><br/>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px;">';
				innerHtml += '		<img class="pointer fade_effect" src="../image/btn_add_message.png" onclick="addRequestMessage(\'' + requestDetail.id + '\');"/>';
				innerHtml += '	</div>';
				
				$("#div_maintain_request_detail").html(innerHtml);
				showRequestMessages(requestDetail.id);
				
				timer_my_request = setInterval(function(){
					if (eval(requestDetail.remainTime) <= 0) {
						$("#remain_time").html("已过期");
					} else {
						requestDetail.remainTime -= 1000;
						$("#remain_time").html(getTimeStr(eval(requestDetail.remainTime)) + "<进行中>");
					}
				}, 1000);
		
				addFadeEffect();
			} catch (e) {
				showTestMessage(e);
			}
		}, "json");
	} catch (e) {
		showTestMessage(e);
	}
}

function showRequestMessages(requestId) {
	try {
		requestUrl = "customer_json_servlet";
		var params = {
			request_method:"get_request_messages",
			request_id:requestId
		};
		
		$.post(requestUrl, params, function(data) {
			try {
				var requestMessages = data.resultData.items;
				var innerHtml = "";
				
				for(var i = 0; requestMessages != null && i < requestMessages.length; i++) {
					innerHtml += '	<div style="margin-top: 15px; height: 27px; width: 380px; font-size: 11pt; background: url(../image/bg_comment_title.png);">';
					innerHtml += '		<span style="width: 115px; float: left;">' + requestMessages[i].userId + '</span>';
					innerHtml += '		<span style="width: 185px; float: right;">' +  requestMessages[i].add_time + '</span>';
					innerHtml += '	</div>';
					innerHtml += '	<div style="padding-top: 2px; font-size: 11pt;" align="left">';
					innerHtml += '		<span>' + requestMessages[i].message + '</span>';
					innerHtml += '	</div>';
				}
				
				$("#request_messages").html(innerHtml);
			} catch (e) {
				showTestMessage(e);
			}
			
		}, "json");
		
	} catch (e) {
		showTestMessage(e);
	}
}

function addRequestMessage(requestId) {
	try {
		requestUrl = "customer_json_servlet";
		var params = {
			request_method:"add_request_message",
			request_id:requestId,
			request_message:$("#request_message").val()
		};
		
		$.post(requestUrl, params, function(data) {
			if (data.code == 0) {
				showRequestMessages(requestId);
			}
		}, "json");
	} catch (e) {
		showTestMessage(e);
	}
}